package api

import (
	"api-gateway/basic/inits"
	__ "api-gateway/basic/order_proto"
	"api-gateway/handler/request"
	"api-gateway/handler/response"
	"github.com/gin-gonic/gin"
)

// CreateOrder
// @Summary      创建订单
// @Description  创建订单
// @Tags         订单模块
// @Accept       json
// @Produce      json
// @Param        user  body      request.CreateOrder  true  "订单参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Router       /order/createOrder [post]
func CreateOrder(c *gin.Context) {
	var req request.CreateOrder
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
	}

	order, err := inits.OrderClient.CreateOrder(c, &__.CreateOrderReq{
		AddressId:   req.AddressId,
		UserId:      req.UserId,
		GoodsId:     req.GoodsId,
		GoodsNum:    req.GoodsNum,
		GoodsName:   req.GoodsName,
		GoodsPrice:  int64(req.GoodsPrice),
		OrderNum:    int32(req.OrderNum),
		OrderPrice:  float32(req.OrderPrice),
		PayCType:    req.PayCType,
		PayStatus:   req.PayStatus,
		OrderStatus: req.OrderStatus,
	})
	if err != nil {
		response.Error(c, 400, "创建订单失败", err.Error())
		return
	}

	response.Success(c, "创建订单成功", order)
}

// UpdateOrder
// @Summary      修改订单信息
// @Description  修改订单信息
// @Tags         订单模块
// @Accept       json
// @Produce      json
// @Param        user  body      request.UpdateOrder  true  "订单参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Router       /order/updateOrder [post]
func UpdateOrder(c *gin.Context) {
	var req request.UpdateOrder
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	order, err := inits.OrderClient.UpdateOrder(c, &__.UpdateOrderReq{
		Id:          req.Id,
		AddressId:   req.AddressId,
		UserId:      req.UserId,
		GoodsId:     req.GoodsId,
		GoodsNum:    req.GoodsNum,
		GoodsName:   req.GoodsName,
		GoodsPrice:  req.GoodsPrice,
		OrderNum:    int32(req.OrderNum),
		OrderPrice:  float32(req.OrderPrice),
		PayCType:    req.PayCType,
		PayStatus:   req.PayStatus,
		OrderStatus: req.OrderStatus,
	})
	if err != nil {
		response.Error(c, 400, "修改订单失败", err.Error())
		return
	}
	response.Success(c, "修改订单成功", order)
}
